package com.cqjtu.education.controller;

import com.cqjtu.education.entity.Chapter;
import com.cqjtu.education.service.ChapterService;
import com.cqjtu.education.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Chapter)表控制层
 *
 * @author makejava
 * @since 2022-04-20 15:46:19
 */
@RestController
@Api(tags = "章节相关接口")
@RequestMapping("chapter")
public class ChapterController {

    @Resource
    private ChapterService chapterService;


    @RequestMapping("/queryFuzzy")
    @ApiOperation(value = "queryFuzzy", notes = "根据课程id(courseId)和章节id(chapterId)查询，章节id为可选参数,可以不填")
    public Result queryFuzzy(@RequestBody Map<String, String> params) {
        return Result.success(this.chapterService.queryFuzzy(params.get("courseId"), params.get("chapterId")));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "通过三个id查询一条数据(courseId,chapterId,sectionId)")
    public Result queryById(@RequestBody Map<String, String> params) {
        return Result.success(this.chapterService.queryById(params.get("courseId"), params.get("chapterId"), params.get("sectionId")));
    }

    /**
     * 新增单条章节
     *
     * @param chapter 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    @ApiOperation(value = "add", notes = "新增单条章节")
    public Result add(@RequestBody Chapter chapter) {
        if (chapterService.insert(chapter) != null) {
            return Result.success("插入成功");
        }
        return Result.fail("插入失败");
    }

    /**
     * 新增多条章节
     *
     * @param chapters 实体
     * @return 新增结果
     */
    @RequestMapping("/addBatch")
    @ApiOperation(value = "addBatch", notes = "新增多条章节")
    public Result addBatch(@RequestBody List<Chapter> chapters) {
        if (chapterService.insertBatch(chapters) != null) {
            return Result.success("插入成功");
        }
        return Result.fail("插入失败");
    }

    /**
     * 编辑数据
     *
     * @param chapter 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "编辑数据")
    public Result edit(@RequestBody Chapter chapter) {
        if (chapterService.update(chapter) != null) {
            return Result.success("更新成功");
        }
        return Result.fail("更新失败");
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除是否成功
     */
    @RequestMapping("/deleteById")
    @ApiOperation(value = "deleteById", notes = "通过三个id删除一条数据")
    public Result deleteById(@RequestBody Map<String, String> params) {
        return Result.success(this.chapterService.deleteById(params.get("courseId"), params.get("chapterId"), params.get("sectionId")));
    }

    /**
     * 批量删除
     *
     * @param
     * @return 删除是否成功
     */
    @RequestMapping("/deleteBatch")
    @ApiOperation(value = "deleteBatch", notes = "批量删除，传入三个id集合")
    public Result deleteBatch(@RequestBody Map<String, List<String>> params) {
        return Result.success(this.chapterService.deleteBatch(params.get("courseId"), params.get("chapterId"), params.get("sectionId")));
    }


}

